<template>
    <div class="table">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item><i class="el-icon-lx-calendar"></i>用户管理</el-breadcrumb-item>
                <el-breadcrumb-item>用户列表</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-form :inline="true" ref="formQuery" :model="formQuery" class="demo-form-inline" label-width="100px">
                    <el-form-item label="用户ID">
                        <el-input v-model="formQuery.userNo" placeholder="用户ID"></el-input>
                    </el-form-item>
                    <el-form-item label="openID">
                        <el-input v-model="formQuery.openID" placeholder="openID"></el-input>
                    </el-form-item>
                    <el-form-item label="userID">
                        <el-input v-model="formQuery.mobile" placeholder="userID"></el-input>
                    </el-form-item>
                    <el-form-item label="状态">
                        <el-select v-model="formQuery.states" placeholder="请选择">
                            <el-option key="0" label="冻结" value="0"></el-option>
                            <el-option key="1" label="正常" value="1"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="创建时间">
                        <el-date-picker
                          v-model="formQuery.creatTime"
                          type="daterange"
                          align="right"
                          unlink-panels
                          range-separator="至"
                          start-placeholder="开始日期"
                          end-placeholder="结束日期">
                        </el-date-picker>
                    </el-form-item>
                    <el-button type="primary" icon="search" @click="onSubmit">查询</el-button>
                </el-form>
            </div>
            <div class="handle-box">
                <el-button type="primary" icon="delete" class="handle-del mr10" @click="handleFreeze()">冻结</el-button>
                <el-button type="primary" icon="delete" class="handle-del mr10" @click="handleUnFreeze()">解冻</el-button>
            </div>
            <el-table :data="data" border class="table" ref="multipleTable" @selection-change="handleSelectionChange" :row-class-name="tableRowClassName">
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="number" label="序号" align="center" width="150">
                </el-table-column>
                <el-table-column prop="userNo" label="用户ID" width="120" align="center">
                </el-table-column>
                <el-table-column prop="openID" label="openID" width="200" align="center">
                </el-table-column>
                <el-table-column prop="userID" label="userID" width="120" align="center">
                </el-table-column>
                <el-table-column prop="mobile" label="手机号" width="200" align="center">
                </el-table-column>
                <el-table-column prop="states" label="状态" width="200" align="center">
                </el-table-column>
                <el-table-column prop="creatTime" label="创建时间" width="200" align="center">
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination background @current-change="handleCurrentChange" layout="prev, pager, next" :total="1000">
                </el-pagination>
            </div>
        </div>

        <!-- 冻结提示框 -->
        <el-dialog title="提示" :visible.sync="freezeVisible" width="300px" center>
            <div class="del-dialog-cnt">是否确定冻结？</div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="freezeVisible = false">取 消</el-button>
                <el-button type="primary" @click="freezeRow">确 定</el-button>
            </span>
        </el-dialog>

        <!-- 解冻提示框 -->
        <el-dialog title="提示" :visible.sync="unFreezeVisible" width="300px" center>
            <div class="del-dialog-cnt">是否确定解冻？</div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="unFreezeVisible = false">取 消</el-button>
                <el-button type="primary" @click="unfreezeRow">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>
<script>
    export default {
        name: 'basetable',
        data() {
            return {
                url: './static/vuetable.json',
                tableData: [{
                    number: '1',
                    userNo: '湖南省基辅迪世界第三',
                    openID: '辅迪',
                    userID: '王小虎',
                    mobile: '13869693636',
                    states: '湖南省',
                    creatTime:'2018-12-15'
                  }, {
                    number: '2',
                    userNo: '湖南省基辅迪世界第三',
                    openID: '辅迪',
                    userID: '王小虎',
                    mobile: '13869693636',
                    states: '湖南省',
                    creatTime:'2018-12-15'
                  }, {
                    number: '3',
                    userNo: '湖南省基辅迪世界第三',
                    openID: '辅迪',
                    userID: '王小虎',
                    mobile: '13869693636',
                    states: '湖南省',
                    creatTime:'2018-12-15'
                  }, {
                    number: '4',
                    userNo: '湖南省基辅迪世界第三',
                    openID: '辅迪',
                    userID: '王小虎',
                    mobile: '13869693636',
                    states: '湖南省',
                    creatTime:'2018-12-15'
                  }],
                cur_page: 1,
                multipleSelection: [],
                select_cate: '',
                select_word: '',
                del_list: [],
                is_search: false,
                freezeVisible:false,
                unFreezeVisible: false,
                formQuery: {
                    userNo: '',
                    openID: '',
                    userID: '',
                    mobile: '',
                    states: '',
                    creatTime:''
                }
            }
        },
        created() {
            // this.getData();
        },
        computed: {
            data() {
                return this.tableData.filter((d) => {
                    let is_del = false;
                    for (let i = 0; i < this.del_list.length; i++) {
                        if (d.name === this.del_list[i].name) {
                            is_del = true;
                            break;
                        }
                    }
                    if (!is_del) {
                        return d;
                    }
                })
            }
        },
        methods: {
            tableRowClassName({row, rowIndex}) {
                if (rowIndex === 1) {
                    return 'warning-row';
                } else if (rowIndex === 3) {
                    return 'success-row';
                }
                return '';
            },
            // 分页导航
            handleCurrentChange(val) {
                this.cur_page = val;
                this.getData();
            },
            getData() { // 获取数据
                // 开发环境使用 easy-mock 数据，正式环境使用 json 文件
                if (process.env.NODE_ENV === 'development') {
                    // this.tableData = res.data.list;
                };
            },
            onSubmit() {
                this.is_search = true;
            },
            filterStates(row, column) {
                let txt = '';
                if(row.states === '0'){
                    txt = '异常'
                } else if(row.states === '1'){
                    txt = '冻结'
                }
                return txt;
            },
            handleFreeze() {
                const length = this.multipleSelection.length;
                if(length === 0){
                    this.$message.error('请选择一条需要编辑的记录！');
                    return;
                }
                if(length>1){
                    this.$message.error('请选择一条需要编辑的记录！');
                    return;
                }
                const record = this.multipleSelection[0];
                const newData = this.tableData;
                const index = newData.findIndex(obj => record.merNo === obj.merNo);
                const item = this.tableData[index];
                this.form = {
                    role:item.role,
                    name: item.name,
                    mobile: item.mobile,
                    states: item.states
                }
                this.freezeVisible = true;
            },
            handleUnFreeze() {
                const length = this.multipleSelection.length;
                if(length === 0){
                    this.$message.error('请选择一条需要编辑的记录！');
                    return;
                }
                if(length>1){
                    this.$message.error('请选择一条需要编辑的记录！');
                    return;
                }
                const record = this.multipleSelection[0];
                const newData = this.tableData;
                const index = newData.findIndex(obj => record.merNo === obj.merNo);
                const item = this.tableData[index];
                this.form = {
                    role:item.role,
                    name: item.name,
                    mobile: item.mobile,
                    states: item.states
                }
                this.unFreezeVisible = true;
            },
            freezeRow() {

            },
            unfreezeRow() {

            },
            handleSelectionChange(val) {
                this.multipleSelection = val;
            }
        }
    }

</script>

<style scoped>
    .handle-box {
        margin-bottom: 20px;
    }

    .handle-select {
        width: 120px;
    }

    .handle-input {
        width: 300px;
        display: inline-block;
    }
    .del-dialog-cnt{
        font-size: 16px;
        text-align: center
    }
    .table{
        width: 100%;
        font-size: 14px;
    }
    .red{
        color: #ff0000;
    }
    .el-table .warning-row {
        background: oldlace;
    }

    .el-table .success-row {
        background: #f0f9eb;
    }
</style>
